package com.hornsun.data.dbo;

import com.hornsun.util.ObjByteUtil;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

/**
 * Created by zhouzhenyan on 2018/5/31.
 */
public class LicenseData {
    public int[] softID = new int[4];
    public int[] normID = new int[64];
    public int permission = 0;
    public byte[] realName = new byte[16];
    public int validTime = -1;
    public int nConnectionCnt = 257;
    public int maxTempAuthTime = 0;
    public PacketData.HeartBeatCode code;
    public int token;

    public static byte[] toBytesArray(LicenseData licenseData) {
        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            DataOutputStream outputStream = new DataOutputStream(stream);
            for (int i=0;i<4; i++) {
                outputStream.writeInt(ObjByteUtil.BytesOrderConv(licenseData.softID[i]));
            }
            for (int i=0;i<64; i++) {
                outputStream.writeInt(ObjByteUtil.BytesOrderConv(licenseData.normID[i]));
            }
            outputStream.writeInt(ObjByteUtil.BytesOrderConv(licenseData.permission));
            outputStream.write(licenseData.realName,0,16);
            outputStream.writeInt(ObjByteUtil.BytesOrderConv(licenseData.validTime));
            outputStream.writeInt(ObjByteUtil.BytesOrderConv(licenseData.nConnectionCnt));
            outputStream.writeInt(ObjByteUtil.BytesOrderConv(licenseData.maxTempAuthTime));
            outputStream.writeInt(ObjByteUtil.BytesOrderConv(licenseData.code.ordinal()));
            outputStream.writeInt(ObjByteUtil.BytesOrderConv(licenseData.token));
            return stream.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
